// pages/elderly/info/updateInfo/updateInfo.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    childid:null,
    elderlyid:null,
    child:null,
    elderly:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.child){
      this.setData({
        childid: options.childid
      })
      app.sendGetRequest('/community/childinfo/byUserId',(res)=>{
        that.setData({
          child: res.rows[0]
        })
      })
      console.log(that.data.child)
    }
    if(options.elder){
      this.setData({
        elderlyid: options.elderlyid
      })
      app.sendGetRequest('/community/elderly/byUserId',(res)=>{
        that.setData({
          elderly: res.rows[0]
        })
      })
      console.log(that.data.elderly)
    }
  },
  submitChild(){
    var that = this
    if(that.data.child.childname.trim() == '' || that.data.child.childname==null){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.child.gender == null){
      wx.showToast({
        title: '性别不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.child.age.toString().trim() == '' || that.data.child.age == null){
      wx.showToast({
        title: '年龄不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.child.address.trim() == '' ||that.data.child.address==null){
      wx.showToast({
        title: '住址不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.child.phone.trim() == '' || that.data.child.phone ==null){
      wx.showToast({
        title: '电话不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.child.familyphone.trim() == '' || that.data.child.familyphone ==null){
      wx.showToast({
        title: '家属电话不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    app.sendPutRequest('/community/childinfo', this.data.child, (res)=>{
      if(res.code==200){
        wx.showToast({
          title: '提交成功',
          duration: 1500,
          success:function () {
            setTimeout(function(){
              var pages = getCurrentPages(); //当前页面
              var beforePage = pages[pages.length - 2]; //前一页
              beforePage.onLoad(); // 执行前一个页面的onLoad方法
              wx.navigateBack({
                delta: 1
              });
            },1500)
          }
        })
      }
    }, (res)=>{
      console.log(res)
    })
  },
  submitElderly(){
    var that = this
    if(that.data.elderly.elderlyname.trim() == '' || that.data.elderly.elderlyname==null){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.elderly.gender == null){
      wx.showToast({
        title: '性别不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.elderly.age.toString().trim() == '' || that.data.elderly.age == null){
      wx.showToast({
        title: '年龄不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.elderly.address.trim() == '' ||that.data.elderly.address==null){
      wx.showToast({
        title: '住址不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.elderly.phone.trim() == '' || that.data.elderly.phone ==null){
      wx.showToast({
        title: '电话不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.elderly.familyphone.trim() == '' || that.data.elderly.familyphone ==null){
      wx.showToast({
        title: '家属电话不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    app.sendPutRequest('/community/elderly', this.data.elderly, (res)=>{
      if(res.code==200){
        wx.showToast({
          title: '提交成功',
          duration: 1500,
          success:function () {
            setTimeout(function(){
              var pages = getCurrentPages(); //当前页面
              var beforePage = pages[pages.length - 2]; //前一页
              beforePage.onLoad(); // 执行前一个页面的onLoad方法
              wx.navigateBack({
                delta: 1
              });
            },1500)
          }
        })
      }
    }, (res)=>{
      console.log(res)
    })
  },
  
  elderlychangeGender(e){
    let v = 'elderly.gender'
    this.setData({
      [v]: parseInt(e.detail.value)
    })
  },
  childchangeGender(e){
    let v = 'child.gender'
    this.setData({
      [v]: parseInt(e.detail.value)
    })
  },
 
  elderlynameChange(e){
    let v = 'elderly.elderlyname'
    this.setData({
      [v]:e.detail.value
    })
  },
  childnameChange(e){
    let v = 'child.childname'
    this.setData({
      [v]:e.detail.value
    })
  },
  elderlyaddressChange(e){
    let v = 'elderly.address'
    this.setData({
      [v]:e.detail.value
    })
  },
  childaddressChange(e){
    let v = 'child.address'
    this.setData({
      [v]:e.detail.value
    })
  },
  elderlyageChange(e){
    let v = 'elderly.age'
    this.setData({
      [v]:e.detail.value
    })
  },
  childageChange(e){
    let v = 'child.age'
    this.setData({
      [v]:e.detail.value
    })
  },
  elderlyphoneChange(e){
    let v = 'elderly.phone'
    this.setData({
      [v]:e.detail.value
    })
  },
  childphoneChange(e){
    let v = 'child.phone'
    
    this.setData({
      [v]:e.detail.value
    })
  },
  elderlyfphoneChange(e){
    let v = 'elderly.familyphone'
    this.setData({
      [v]:e.detail.value
    })
  },
  childfphoneChange(e){
    let v = 'child.familyphone'
    this.setData({
      [v]:e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})